<template>
  <view class="page-wrapper global-wrapper bgF2">
    <JHeaderTitle title="进件管理" bgColor="#f2f2f2" />
    <JSearchInput place="搜索商户名、商户号、进件单号" ref="searchInfo" @search="search" @resetSearch="resetSearch" />
    <ApplyHeader @selected="selected" />
    <block v-for="v in useDataResult.dataList" :key="v.applyId">
      <ApplyCard
        :ifName="v.ifName"
        :icon="v.icon"
        :bgColor="v.bgColor"
        :createdAt="v.createdAt"
        :mchFullName="v.mchFullName"
        :mchNo="v.mchNo"
        :applyId="v.applyId"
        :state="v.state"
        :applyErrorInfo="v.applyErrorInfo"
        :ifCode="v.ifCode"
        @click="toPage(v.ifCode, v.state, v.ifName, v.applyId, v.applyErrorInfo, v.mchNo, v.channelVar1)"
        @getNewState="getNewState(v)"
      />
      <view></view>
    </block>
    <jeepayListNull :isShow="true" :list="useDataResult.dataList.length" />
    <view class="block"></view>
    <view class="apply-footer ButtonBor">
      <JButton pd="30rpx  30rpx 50rpx 30rpx" pdTop="0" @HandleTouch="goToApply">发起进件</JButton>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad, onBackPress, onShow } from '@dcloudio/uni-app'
import { $applyList, $getMchApplymentChannelState } from '@/http/apiManager.js'
import useGetList from '@/hooks/useList.js'
import JHeaderTitle from '@/components/newComponents/JHeaderTitle/JHeaderTitle'
import JSearchInput from '@/components/newComponents/JSearchInput/JSearchInput'
import jeepayListNull from '@/components/jeepayListNull/jeepayListNull.vue'
import JButton from '@/components/newComponents/JButton/JButton'
import ApplyHeader from './components/ApplyHeader.vue'
import ApplyCard from './components/ApplyCard.vue'
const { useDataResult, getList } = useGetList({
  requestFun: $applyList,
  params: { state: '' },
})
const searchInfo = ref(null)
const params = ref({
  state: '',
  mch: '',
})
const search = (data) => {
  params.value.mch = data
  getList(params.value)
}
const resetSearch = (data) => {
  params.value.mch = ''
  getList(params.value)
}
const selected = (val) => {
  params.value.state = val
  getList(params.value)
}
const goToApply = () => {
  uni.navigateTo({
    url: './selectChannel',
  })
}
function toPage(code, state, name, applyId, src, mchNo, channelVar1) {
  let isView = state == 0 || state == 3 || state == 8
  uni.navigateTo({ url: `./applyDetailWeb?applyId=${applyId}&isView=${isView ? 0 : 1}` })
}

function toH5ApplyOptionPage(applyId, configPage) {
  console.log(applyId, configPage)
  uni.navigateTo({ url: `./applyDetailH5ApplyOption?applyId=${applyId}&configPage=${configPage}` })
}
// 获取进件最新状态
const getNewState = (val) => {
  $getMchApplymentChannelState(val.applyId)
    .then(({ bizData }) => {
      val.state = bizData.state
      uni.showToast({
        title: '获取最新结果成功',
        icon: 'none',
        mask: true,
      })
    })
    .catch((err) => {
      uni.showToast({
        title: '获取最新结果失败请稍后重试',
        icon: 'none',
        mask: true,
      })
    })
}
onBackPress(() => {
  if (searchInfo.value.searchText !== '' || params.value.mch !== '') {
    searchInfo.value.searchText = ''
    params.value.state = ''
    params.value.mch = ''
    getList(params.value)
    return true
  } else {
    return false
  }
})
</script>

<style lang="scss" scoped>
.page-wrapper {
  .block {
    height: 210rpx;
  }
  .apply-footer {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
  }
}
</style>
